package io.github.talelin.latticy.service.K;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.K.BGatewayDTO;
import io.github.talelin.latticy.mapper.K.BGatewayMapper;
import io.github.talelin.latticy.model.K.BGatewayDO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author generator@TaleLin
 * @since 2021-06-03
 */
@Service
public class BGatewayService extends ServiceImpl<BGatewayMapper, BGatewayDO> {

    @Autowired
    private BGatewayMapper bGatewayMapper;

    public BGatewayDO getById(Integer id) {
        BGatewayDO b_gateway = bGatewayMapper.selectById(id);
        return b_gateway;
    }

    public void update(BGatewayDTO dto, Long id) {
        BGatewayDO BGatewayDO = this.getById(id);
        if (BGatewayDO == null) {
            throw new NotFoundException(70000);
        }
        BeanUtils.copyProperties(dto, BGatewayDO);
        this.updateById(BGatewayDO);
    }

    public void delete(long id) {
        BGatewayDO exist = this.getById(id);
        if (exist == null) {
            throw new NotFoundException(70000);
        }
        this.getBaseMapper().deleteById(id);
    }

    public IPage<BGatewayDO> seachBGateway(
            Page<BGatewayDO> pager,
            String keyword){
        QueryWrapper<BGatewayDO> wrapper = new QueryWrapper<>();
        wrapper.like("name",keyword)
                .or().like("location",keyword)
                .or().like("uuid",keyword);
        IPage<BGatewayDO> iPage = bGatewayMapper.selectPage(pager,wrapper);
        return iPage;
    }
}
